package com.zhouchao.mvp.login;

import android.util.Log;

import com.zhouchao.mvp.Entity.User;

import java.util.Random;

/**
 * 描述：
 * zhougan
 * 2019/2/28
 **/
public class LoginModel implements LoginContract.ILoginModel {
    private static final String TAG = ">>>LoginModel";
    public static final String INVALITE_SUCCESS="success";
    public static final String INVALITE_NAME_ERROR="用户名必须大于6小于12";
    public static final String INVALITE_PASSWORD_ERROR="密码不能为纯数字,且大于6小于12";
    public static final String INVALITE_USER_NULL="user不能为null";

    private static LoginModel instance;

    private LoginModel() {

    }

    public static LoginModel getInstance(){
        if(instance==null){
            synchronized (LoginModel.class){
                if(instance==null){
                    instance=new LoginModel();
                }
            }
        }

        return instance;
    }


    @Override
    public void login( User user, final Callback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "run: 开始登陆");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int i = new Random().nextInt(4);

                Log.i(TAG, "run: 登陆完成");
                if(i==1){
                    callback.onFailure("登陆失败");
                }else{
                    callback.onSuccess();
                }

            }
        }).start();

    }

    @Override
    public String invalite(User user) {
        if(user==null){
            return INVALITE_USER_NULL;
        }else if(user.getName()==null || user.getName().length()<6 || user.getName().length()>12){
            return INVALITE_NAME_ERROR;
        }else if(user.getPassword()==null || user.getPassword().length()<6 || user.getPassword().length()>12){
            return INVALITE_PASSWORD_ERROR;
        }else{
            try {
                int i = Integer.parseInt(user.getPassword());
                return INVALITE_PASSWORD_ERROR;
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }


        return INVALITE_SUCCESS;
    }
}
